package com.chartboost.sdk.impl;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Build.VERSION;
import android.util.Log;
import android.util.SparseArray;
import com.chartboost.sdk.Chartboost;
import com.chartboost.sdk.Libraries.d;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.scheme.SocketFactory;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

public class j
{
  private static int g = 0;
  private static HttpClient h = null;
  public String a;
  public c b;
  public String c;
  public SparseArray<b> d;
  public int e = 1;
  public String f = "Loading...";

  public j(String paramString1, c paramc, String paramString2)
  {
    if (paramString1 == null)
      paramString1 = "https://www.chartboost.com";
    this.a = paramString1;
    this.b = paramc;
    this.c = paramString2;
    this.d = new SparseArray();
  }

  private static String b(Application paramApplication, String paramString)
  {
    try
    {
      String str = paramApplication.getPackageManager().getPackageInfo(paramApplication.getPackageName(), 0).versionName;
      StringBuilder localStringBuilder = new StringBuilder();
      localStringBuilder.append(paramApplication.getPackageName());
      localStringBuilder.append("/");
      localStringBuilder.append(str);
      localStringBuilder.append(" (");
      localStringBuilder.append("Linux; U; Android ");
      localStringBuilder.append(Build.VERSION.RELEASE);
      localStringBuilder.append("; ");
      localStringBuilder.append(Locale.getDefault());
      localStringBuilder.append("; ");
      localStringBuilder.append(Build.PRODUCT);
      localStringBuilder.append(")");
      if (paramString != null)
      {
        localStringBuilder.append(" ");
        localStringBuilder.append(paramString);
      }
      return localStringBuilder.toString();
    }
    catch (PackageManager.NameNotFoundException localNameNotFoundException)
    {
      throw new RuntimeException(localNameNotFoundException);
    }
  }

  public static HttpClient b()
  {
    if (h != null)
      return h;
    try
    {
      h = new DefaultHttpClient()
      {
        protected SocketFactory a()
        {
          try
          {
            Class localClass1 = Class.forName("android.net.SSLSessionCache");
            Constructor localConstructor = localClass1.getConstructor(new Class[] { Context.class });
            Object[] arrayOfObject1 = new Object[1];
            arrayOfObject1[0] = j.this;
            Object localObject = localConstructor.newInstance(arrayOfObject1);
            Class localClass2 = Class.forName("android.net.SSLCertificateSocketFactory");
            Class[] arrayOfClass = new Class[2];
            arrayOfClass[0] = Integer.TYPE;
            arrayOfClass[1] = localClass1;
            Method localMethod = localClass2.getMethod("getHttpSocketFactory", arrayOfClass);
            Object[] arrayOfObject2 = new Object[2];
            arrayOfObject2[0] = Integer.valueOf(Chartboost.sharedChartboost().getTimeout());
            arrayOfObject2[1] = localObject;
            SocketFactory localSocketFactory = (SocketFactory)localMethod.invoke(null, arrayOfObject2);
            return localSocketFactory;
          }
          catch (Exception localException)
          {
          }
          return SSLSocketFactory.getSocketFactory();
        }

        protected ClientConnectionManager createClientConnectionManager()
        {
          SchemeRegistry localSchemeRegistry = new SchemeRegistry();
          localSchemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
          localSchemeRegistry.register(new Scheme("https", a(), 443));
          HttpParams localHttpParams = getParams();
          HttpConnectionParams.setConnectionTimeout(localHttpParams, Chartboost.sharedChartboost().getTimeout());
          HttpConnectionParams.setSoTimeout(localHttpParams, Chartboost.sharedChartboost().getTimeout());
          HttpProtocolParams.setUserAgent(localHttpParams, j.a(j.this, HttpProtocolParams.getUserAgent(localHttpParams)));
          return new ThreadSafeClientConnManager(localHttpParams, localSchemeRegistry);
        }
      };
      HttpClient localHttpClient2 = h;
      return localHttpClient2;
    }
    catch (Exception localException1)
    {
      try
      {
        DefaultHttpClient localDefaultHttpClient = new DefaultHttpClient();
        ClientConnectionManager localClientConnectionManager = localDefaultHttpClient.getConnectionManager();
        HttpParams localHttpParams = localDefaultHttpClient.getParams();
        h = new DefaultHttpClient(new ThreadSafeClientConnManager(localHttpParams, localClientConnectionManager.getSchemeRegistry()), localHttpParams);
        HttpClient localHttpClient1 = h;
        return localHttpClient1;
      }
      catch (Exception localException2)
      {
        h = new DefaultHttpClient();
      }
    }
    return h;
  }

  private void b(k paramk)
  {
    SharedPreferences localSharedPreferences;
    String str1;
    if ((paramk.i) && (this.c != null))
    {
      localSharedPreferences = d.a();
      str1 = "CBQueuedRequests-" + this.c;
    }
    while (true)
    {
      try
      {
        JSONObject localJSONObject = paramk.d();
        if (localJSONObject == null)
          break;
        String str2 = localSharedPreferences.getString(str1, null);
        if (str2 != null)
          try
          {
            localJSONArray = new JSONArray(new JSONTokener(str2));
            localJSONArray.put(localJSONObject);
            SharedPreferences.Editor localEditor = localSharedPreferences.edit();
            localEditor.putString(str1, localJSONArray.toString());
            localEditor.commit();
            return;
          }
          catch (Exception localException2)
          {
            Log.w("Chartboost", "Failure reading saved request list");
            localJSONArray = new JSONArray();
            continue;
          }
      }
      catch (Exception localException1)
      {
        Log.w("Chartboost", "Unable to save failed request");
        return;
      }
      JSONArray localJSONArray = new JSONArray();
      continue;
      if (this.b != null)
        this.b.a(paramk);
    }
  }

  public void a()
  {
    if (!l.a());
    while (true)
    {
      return;
      SharedPreferences localSharedPreferences = d.a();
      String str1 = "CBQueuedRequests-" + this.c;
      String str2 = localSharedPreferences.getString(str1, null);
      if (str2 != null)
      {
        SharedPreferences.Editor localEditor = localSharedPreferences.edit();
        localEditor.putString(str1, null);
        localEditor.commit();
        try
        {
          JSONArray localJSONArray = new JSONArray(new JSONTokener(str2));
          int i = 0;
          while (true)
          {
            int j = localJSONArray.length();
            if (i >= j)
              break;
            try
            {
              k localk = k.a(localJSONArray.getJSONObject(i));
              if (localk != null)
                a(localk);
              i++;
            }
            catch (Exception localException2)
            {
              while (true)
                Log.w("Chartboost", "Retrying request failed");
            }
          }
        }
        catch (Exception localException1)
        {
          Log.w("Chartboost", "Retrying request list failed");
        }
      }
    }
  }

  public void a(k paramk)
  {
    if (!l.a())
    {
      b(paramk);
      return;
    }
    int i = this.e;
    this.e = (i + 1);
    b localb = new b(paramk, null);
    localb.c = Integer.valueOf(i);
    this.d.put(i, localb);
    new a().execute(new b[] { localb });
  }

  protected class a extends AsyncTask<j.b, Void, j.b>
  {
    protected a()
    {
    }

    protected j.b a(j.b[] paramArrayOfb)
    {
      j.b localb = paramArrayOfb[0];
      k localk = localb.a;
      HttpPost localHttpPost = new HttpPost(j.this.a + localk.a());
      localHttpPost.setHeader("Content-Type", "application/json; charset=UTF-8");
      localHttpPost.setHeader("Accept", "application/json; charset=UTF-8");
      localHttpPost.setHeader("X-Chartboost-Client", "Chartboost-Android-SDK 3.1.5");
      localHttpPost.setHeader("X-Chartboost-API", "3.1.5");
      Map localMap = localk.c();
      Iterator localIterator;
      int i;
      HttpResponse localHttpResponse;
      if (localMap != null)
      {
        localIterator = localMap.keySet().iterator();
        if (localIterator.hasNext());
      }
      else
      {
        synchronized (j.this)
        {
          i = 1 + j.c();
          j.a(i);
          localHttpResponse = null;
        }
      }
      while (true)
      {
        int j;
        StringBuilder localStringBuilder;
        String str1;
        try
        {
          JSONObject localJSONObject = localk.e;
          localHttpResponse = null;
          if (localJSONObject != null)
          {
            StringEntity localStringEntity = new StringEntity(localk.e.toString());
            localStringEntity.setContentType(new BasicHeader("Content-Type", "application/json"));
            localHttpPost.setEntity(localStringEntity);
            localHttpResponse = j.b().execute(localHttpPost);
            j = localHttpResponse.getStatusLine().getStatusCode();
            if ((j >= 300) || (j < 200))
              break label487;
            BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(localHttpResponse.getEntity().getContent(), "UTF-8"), 2048);
            localStringBuilder = new StringBuilder();
            str1 = localBufferedReader.readLine();
            if (str1 != null)
              break label471;
            localBufferedReader.close();
            localk.j = new JSONObject(new JSONTokener(localStringBuilder.toString()));
            localHttpResponse.getEntity().consumeContent();
            return localb;
            String str2 = (String)localIterator.next();
            localHttpPost.setHeader(str2, (String)localMap.get(str2));
            break;
            localObject = finally;
            throw localObject;
          }
          Log.i("HTTP Request Body " + i + " " + localk.b, "<empty>");
          continue;
        }
        catch (Exception localException1)
        {
          Log.e("Chartboost", "Exception on http request: " + localException1.getLocalizedMessage());
          if (localHttpResponse == null);
        }
        try
        {
          if (localHttpResponse.getEntity() != null)
            localHttpResponse.getEntity().consumeContent();
          label469: return localb;
          label471: localStringBuilder.append(str1).append("\n");
          continue;
          label487: Log.w("Chartboost", "Request failed. Response code: " + j + ", body: " + localHttpResponse);
          localHttpResponse.getEntity().consumeContent();
          return localb;
        }
        catch (Exception localException2)
        {
          break label469;
        }
      }
    }

    protected void a(j.b paramb)
    {
      j.this.d.remove(paramb.c.intValue());
      if (paramb.a.j != null)
      {
        if (j.this.b != null)
          j.this.b.a(paramb.a.j, paramb.a);
        return;
      }
      j.a(j.this, paramb.a);
    }

    protected void onPreExecute()
    {
    }
  }

  protected class b
    implements Serializable
  {
    public k a;
    public JSONObject b;
    public Integer c;

    public b(k paramJSONObject, JSONObject arg3)
    {
      this.a = paramJSONObject;
      Object localObject;
      this.b = localObject;
    }
  }

  public static abstract interface c
  {
    public abstract void a(k paramk);

    public abstract void a(JSONObject paramJSONObject, k paramk);
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.chartboost.sdk.impl.j
 * JD-Core Version:    0.6.2
 */